// pages/list/list.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    input: '',
    todos: [{
      "name": "任务一",
      "id": 1,
      "completad": true
    }, {
      "name": "任务二",
      "id": 2,
      "completad": false
    }, {
      "name": "任务三",
      "id": 3,
      "completad": true
    }, {
      "name": "任务四",
      "id": 4,
      "completad": false
    }],
    leftCount: 2,
    allCompleted: false
  },
  addTodoHandle() {
    if (!this.data.input) {
      return false
    }
    var todos = this.data.todos;
    todos.push({
      name: this.data.input,
      completad: false,
    })

    this.setData({
      todos: todos,
      input: '',
      leftCount: this.data.leftCount + 1
    })
  },
  inputChangeHandle(e) {
    // console.log(e);
    this.setData({
      input: e.detail.value
    })
  },
  //切换当前中item的完成状态
  toggleTodoHandle(e) {
    var item = this.data.todos[e.currentTarget.dataset.index];
    console.log(item)
    item.completad = !item.completad;
    var leftCount = this.data.leftCount + (item.completad ? -1 : 1)
    this.setData({
      todos: this.data.todos,
      leftCount: leftCount
    })
  },
  removeTodoHandle(e) {
    var item = this.data.todos.splice(e.currentTarget.dataset.index, 1)[0];
    var leftCount = this.data.leftCount - (item.completad ? 0 : 1)
    this.setData({
      todos: this.data.todos,
      leftCount: leftCount
    })
  },
  toggleAllHandle() {
    this.data.allCompleted = !this.data.allCompleted;
    var todos = this.data.todos;
    var that = this;
    todos.forEach((item) => {
      item.completad = this.data.allCompleted;
    })
    var leftCount = this.data.allCompleted ? 0 : this.data.todos.length
    this.setData({
      todos: todos,
      leftCount: leftCount
    })
  },
  chearHandle() {
    var todos = this.data.todos.filter(function (item) {
      return !item.completad
    })
    this.setData({
      todos: todos
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})